package com.dengyin2000.javaeye.ordering.action;

import java.util.Date;

import org.hibernate.Session;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.faces.FacesMessages;

import com.dengyin2000.javaeye.ordering.entity.Role;
import com.dengyin2000.javaeye.ordering.entity.User;

@Name("registerAction")
public class RegisterAction {
	
	@In(value="user", create=true)
	private User newUser;
	private String repassword;

    
    @In(value="orderingDatabase")
    Session session;
    
    @In
    FacesMessages facesMessages;
    
	public User getNewUser() {
		return newUser;
	}

	public void setNewUser(User newUser) {
		this.newUser = newUser;
	}
	
	public void invaild() {
		facesMessages.add("表单验证有错误，请再尝试一次！");
	}
	
	public String registerUser() {
		if (repassword.equals(newUser.getPassword())) {
			newUser.setRegisteDate(new Date());
			Role userRole = (Role) session.load(Role.class, new Long(1));
			newUser.addRole(userRole);
			session.save(newUser);
			facesMessages.add("#{newUser.loginName} 注册成功！");
			return "success";
		}else {
			facesMessages.addToControl("repassword", "密码输入不一致！");
			return "failed";
		}
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
}
